const app = require("express").Router();
const { Pubilc, Status } = require("../model/index");
const Sequelize = require("sequelize");
const Op = Sequelize.Op;

// 发布
app.post("/public", (req, res) => {
  let data = req.body;
  if (!data.title) {
    res.send({ code: 1, msg: "请输入标题" });
    return;
  }
  if (!data.describe) {
    res.send({ code: 1, msg: "请输入描述" });
    return;
  }
  Pubilc.create(data).then((ret) => {
    if (ret) {
      res.send({ code: 0, data: ret });
    } else {
      res.send({ code: -1, msg: "发布失败" });
    }
  });
});

// 修改发布
app.post("/updatePublic", (req, res) => {
  let data = req.body;
  if (!data.title) {
    res.send({ code: 1, msg: "请输入标题" });
    return;
  }
  if (!data.describe) {
    res.send({ code: 1, msg: "请输入描述" });
    return;
  }
  Pubilc.update(data, {
    where: {
      id: data.id,
    },
  }).then((ret) => {
    if (ret) {
      res.send({ code: 0, data: ret });
    } else {
      res.send({ code: -1, msg: "修改失败" });
    }
  });
});

// 删除发布
app.get("/delPubilc", (req, res) => {
  Pubilc.destroy({
    where: {
      id: req.query.id,
      userid: req.query.userid,
    },
  }).then((ret) => {
    if (ret) {
      res.send({ code: 0, data: ret });
    } else {
      res.send({ code: -1, msg: "删除失败" });
    }
  });
});

// 查发布
app.get("/publicList", (req, res) => {
  const { title, page, id } = req.query;
  console.log(id, "id。。。");
  let offset = (page - 1) * 10;
  if (!page) {
    res.send({ code: -1, msg: "页码未传" });
    return;
  }

  let where = {
    title: {
      // 模糊查询
      [Op.like]: "%" + title + "%",
    },
  };
  Pubilc.findAndCountAll({
    order: [["createDate", "DESC"]],
    limit: 10,
    offset,
    where,
  }).then((ret) => {
    if (ret) {
      res.send({ code: 0, data: ret });
    } else {
      res.send({ code: -1, msg: "查询失败" });
    }
  });
});

// 查发布 byId
app.get("/publicListById", (req, res) => {
  const { id } = req.query;
  let where = {
    id: id,
  };
  Pubilc.findAndCountAll({
    where,
  }).then((ret) => {
    if (ret) {
      res.send({ code: 0, data: ret });
    } else {
      res.send({ code: -1, msg: "查询失败" });
    }
  });
});

// 查询状态
app.get("/getstate", (req, res) => {
  const { userid, publicid } = req.query;
  Status.findAndCountAll({
    where: {
      userid,
      publicid,
    },
  }).then((ret) => {
    res.send({ code: 0, data: ret });
  });
});

// 修改状态种草 闹钟
app.post("/updatestate", (req, res) => {
  // state 1 新增操作 2 取消操作 type对饮status表字段   type2 对饮pulie 列表字段 type2 == 1 种草 =2闹钟
  const { userid, publicid, type, type2, state } = req.body;
  Status.findAndCountAll({
    where: {
      userid,
      publicid,
    },
  }).then((ret) => {
    // 更新publich 列表数字字段
    if (state == 1 && type2 == 1) {
      // 种草
      Pubilc.findAndCountAll({ where: { id: publicid } }).then((zret) => {
        zret.rows[0].increment("cao").then((cao) => {
          console.log("种草+1成功");
        });
      });
    } else if (state == 0 && type2 == 1) {
      // 拔草
      Pubilc.findAndCountAll({ where: { id: publicid } }).then((zret) => {
        zret.rows[0].decrement("cao").then((cao) => {
          console.log("种草-1成功");
        });
      });
    } else if (state == 1 && type2 == 2) {
      // 设置闹钟
      Pubilc.findAndCountAll({ where: { id: publicid } }).then((zret) => {
        zret.rows[0].increment("nao").then((cao) => {
          console.log("闹钟+1成功");
        });
      });
    } else if (state == 0 && type2 == 2) {
      // 取消闹钟
      Pubilc.findAndCountAll({ where: { id: publicid } }).then((zret) => {
        zret.rows[0].decrement("nao").then((cao) => {
          console.log("闹钟-1成功");
        });
      });
    }

    // 更行Status表
    // 数据有记录直接更新
    if (ret && ret.rows.length) {
      let obj = {};
      obj[type] = state;
      Status.update(obj, {
        where: {
          userid,
          publicid,
        },
      }).then((ret1) => {
        res.send({ code: 0, msg: "操1作成功", data: ret1 });
        if (ret1 && ret1[0] == 1) {
          res.send({ code: 0, msg: "操作成功" });
        } else {
          res.send({ code: -1, msg: "状态修改失败" });
        }
      });
    } else {
      // 数据库没记录新增一条
      let data = {
        userid: req.body.userid,
        publicid: req.body.publicid,
      };
      data[type] = state;
      Status.create(data).then((ret) => {
        if (ret) {
          res.send({ code: 0, data: ret });
        } else {
          res.send({ code: 0, msg: "操作失败" });
        }
      });
    }
  });
});

// 查询用户种草数目
app.get("/getcaonums", (req, res) => {
  Status.findAndCountAll({
    where: {
      userid: req.query.userid,
      iszc: 1,
    },
  }).then((ret) => {
    if (ret) {
      res.send({ code: 0, data: ret });
    } else {
      res.send({ code: -1, msg: "查询失败" });
    }
  });
});

// 查询用户闹钟数
app.get("/getnaonums", (req, res) => {
  Status.findAndCountAll({
    where: {
      userid: req.query.userid,
      isnz: 1,
    },
  }).then((ret) => {
    if (ret) {
      res.send({ code: 0, data: ret });
    } else {
      res.send({ code: -1, msg: "查询失败" });
    }
  });
});

// 查询关联 我的种草列表
app.get("/mycaolist", (req, res) => {
  const { page} = req.query;
  let offset = (page - 1) * 10;
  if (!page) {
    res.send({ code: -1, msg: "页码未传" });
    return;
  }
  // 根据status 里面publiid 和user_publicid表里面id一样查出来
  Status.belongsTo(Pubilc, { foreignKey: 'publicid',  })
  Status.findAll({
    limit: 100,
    offset,
    attributes: ['publicid',"iszc"],
    include: [{
        model: Pubilc,
        attributes: ['id','title','describe','url','header_url','username','stars','cao','nao']
    }],
    where:{
      iszc:1,
      userid:req.query.userid
    },
   
  }).then(ret=>{
    if(ret && ret.length){
      res.send({code:0,data:ret})
    }else{
      res.send({code:-1,msg:'查询失败'})
    }
  })
});


// 查询关联 我的闹钟列表
app.get("/mynaolist", (req, res) => {
  const { page} = req.query;
  let offset = (page - 1) * 10;
  if (!page) {
    res.send({ code: -1, msg: "页码未传" });
    return;
  }
  // 根据status 里面publiid 和user_publicid表里面id一样查出来
  Status.belongsTo(Pubilc, { foreignKey: 'publicid',  })
  Status.findAll({
    attributes: ['publicid',"isnz"],
    include: [{
        model: Pubilc,
        attributes: ['id','title','describe','url','header_url','username','stars','cao','nao']
    }],
    where:{
      isnz:1,
      userid:req.query.userid
    },
    limit: 100,
    offset,
  }).then(ret=>{
    if(ret && ret.length){
      res.send({code:0,data:ret})
    }else{
      res.send({code:-1,msg:'查询失败'})
    }
  })
});




module.exports = app;
